package powerControl.web.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import powerControl.entity.User;
import powerControl.service.IUserService;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport implements SessionAware{
	
	private Map<String, Object> session;
	
	private User user;
	
	private IUserService userService;
	/**
	 * 测试方法，可删除
	 * @return
	 */
	public String test() {
		return LOGIN;
	}
	
	/**
	 * 登录方法
	 * @return
	 */
	public String login() {
		String result = "";
		//System.out.println(user.getName() + user.getPassword() );
		User loginUser = userService.findByNameAndPassword(user);
		session.put("loginUser", loginUser);
		if (loginUser == null) {
			System.out.println(loginUser.getName() + "\n\n\n  ------------登录失败------------\n\n\n");
			//登录失败后跳转到登录页
			result = LOGIN;
		}else {
			System.out.println(loginUser.getName() + "\n\n\n  ------------登录成功------------\n\n\n");
			//登录成功后跳转到主页
			result = "main";
		}
		return result;
	}
	
	
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public User getUser() {
		return user;
	}
	
	public void setUser(User user) {
		this.user = user;
	}
	
	public Map<String, Object> getSession() {
		return session;
	}
	
	/**
	 * @param userService the userService to set
	 */
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	/**
	 * @return the userService
	 */
	public IUserService getUserService() {
		return userService;
	}
}